package com.algorithm.list;
//双向链表
public class DoubleLinedList {

	Node mNodeHead;
	Node mNodeLast;

	public void insert(Node mNode){

		if(mNodeHead==null){
			mNodeHead = mNode;
		}

		if(mNodeLast!=null){
			mNodeLast.next = mNode;
			mNode.pre = mNodeLast;
		}
		mNodeLast=mNode;
	}

	public void remove(Node mNode){
		Node mPreNode = mNode.pre;
		Node mNextNode = mNode.next;

		if(mNode.pre==null){
			mNodeHead = mNode.next;
		}else{
			mPreNode.next = mNextNode;
		}

	}

	static class Node{
		Node pre;
		Node next;
		String value;
	}

	@Override
	public String toString() {
		StringBuilder mBuilder = new StringBuilder();
		DoubleLinedList.Node mNodeInteRupt = mNodeHead;
		while(mNodeInteRupt!=null){
			mBuilder.append(mNodeInteRupt.value);
			mNodeInteRupt = mNodeInteRupt.next;
			if(mNodeInteRupt!=null)
				mBuilder.append("->");
		}
		return mBuilder.toString();
	}


	public static void main(String args[]){
		DoubleLinedList mList = new DoubleLinedList();
		Node mNode = new Node();
		mNode.value= "first";
		mList.insert(mNode);

		Node mNode1 = new Node();
		mNode1.value= "second";
		mList.insert(mNode1);

		Node mNode2 = new Node();
		mNode2.value= "third";
		mList.insert(mNode2);


		mList.remove(mNode2);
		System.out.println(mList.toString());
	}
}
